package tienda.zapateria.siszap.da;

import java.sql.Connection;
import tienda.zapateria.siszap.be.BEUbigeo;
import java.util.ArrayList;
import org.rx.cr.dao.DAOAbstract;

public class DAUbigeo extends DAOAbstract<BEUbigeo> {

    public DAUbigeo(Connection c) {
        setConnection(c);
    }

    @Override
    public int registrar(BEUbigeo be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_ins_ubigeo");
            setParameterString(be.getDepartamento());
            setParameterString(be.getProvincia());
            setParameterString(be.getDistrito());

            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int actualizarRegistro(BEUbigeo be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_upd_ubigeo");
            setParameterInt(be.getUbigeoid());
            setParameterString(be.getDepartamento());
            setParameterString(be.getProvincia());
            setParameterString(be.getDistrito());

            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int eliminarRegistro(BEUbigeo be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_del_ubigeo");
            setParameterInt(be.getUbigeoid());
            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public ArrayList<BEUbigeo> buscarReferencia(String string) throws Exception {
        try {
            clear();
            setStoreProcedure("zapateria.sp_sel_ubigeo");
            setParameterString(string);
            commitc();
            while (existResult()) {
                BEUbigeo be = new BEUbigeo();
                be.setUbigeoid(getDataInt(1));
                be.setDepartamento(getDataString(2));
                be.setProvincia(getDataString(3));
                be.setDistrito(getDataString(4));

                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public ArrayList<BEUbigeo> listar() throws Exception {
        try {
            clear();
            setStoreProcedure("zapateria.sp_sel_ubigeo");
            setParameterString("");
            commitc();
            while (existResult()) {
                BEUbigeo be = new BEUbigeo();
                be.setUbigeoid(getDataInt(1));
                be.setDepartamento(getDataString(2));
                be.setProvincia(getDataString(3));
                be.setDistrito(getDataString(4));

                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public BEUbigeo buscar(int i) throws Exception {
        try {
            BEUbigeo be = null;
            clear();
            setStoreProcedure("zapateria.sp_sel_ubigeo_id");
            setParameterInt(i);
            commitc();
            while (existResult()) {
                be = new BEUbigeo();
                be.setUbigeoid(getDataInt(1));
                be.setDepartamento(getDataString(2));
                be.setProvincia(getDataString(3));
                be.setDistrito(getDataString(4));

            }
            return be;
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    public ArrayList<BEUbigeo> listarDepartamento() throws Exception {
        try {
            clear();
            setStoreProcedure("zapateria.sp_listar_departamento");
            commitc();
            while (existResult()) {
                BEUbigeo be = new BEUbigeo();
                be.setDepartamento(getDataString(1));
                add(be);
            }
            close();
            return getLista();
        } catch (Exception ex) {
            rollback(ex);
            return null;
        } finally {
        }
    }

    public ArrayList<BEUbigeo> listarProvinciaDepartamento(String departamento) throws Exception {
        try {
            clear();
            setStoreProcedure("zapateria.sp_listar_provincia_departamento");
            setParameterString(departamento);
            commitc();
            while (existResult()) {
                BEUbigeo be = new BEUbigeo();
                be.setProvincia(getDataString(1));
                add(be);
            }
            close();
            return getLista();
        } catch (Exception ex) {
            rollback(ex);
            return null;
        } finally {
        }
    }

    public ArrayList<BEUbigeo> listarDistritoProvinciaDepartamento(String departamento, String provincia) throws Exception {
        try {
            clear();
            setStoreProcedure("zapateria.sp_listar_distrito_provincia_departamento");
            setParameterString(departamento);
            setParameterString(provincia);
            commitc();
            while (existResult()) {
                BEUbigeo be = new BEUbigeo();
                be.setUbigeoid(getDataInt(1));
                be.setDistrito(getDataString(2));
                add(be);
            }
            close();
            return getLista();
        } catch (Exception ex) {
            rollback(ex);
            return null;
        } finally {
        }
    }

}
